'use strict';
// 
const db = uniCloud.database();

exports.main = async (event, context) => {
	const { type } = event;
	if(type === 'openid'){
		try{
			const {code} = event;
			const appid = 'wx453e20a37c27652e';
			const secret = '406b9eba2e0ce02bf4c209b28536d918';
			const apiUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&js_code='+code+'&grant_type=authorization_code';
			const res = await uniCloud.httpclient.request(apiUrl, {
					method: 'GET',
					data: {},
					contentType: 'json', // 指定以application/json发送data内的数据
					dataType: 'json' // 指定返回值为json格式，自动进行parse
				})
			console.log('获取用户信息',res);
			const getSign = await db.collection('sign').where({
				openid: res.data.openid
			}).get();
			
			console.log('获取登录中有没有openid',getSign);
			if(getSign.data.length>0){
				return{
					code: 200,
					status: 'S',
					message:'用户已登录过',
					data: getSign
				}
			}else{
				return res
			}
		}catch(e){
			console.log('错误',e)
			return{
				code: 2000,
				status: 'N',
				message:'网络错误，请重新启动小程序',
				data: []
				
			}
		}
		
	}
	if( type === 'sign'){
		const { name, password } = event;
		try{
			const getAdd = await db.collection('sign').where({
				data:{
					name
				}
			}).get();
			console.log('获取注册的信息------',getAdd)
			const status = getAdd.data.every(item=> {
				console.log(item.data);
				item.data.name !=name
			});
			
			console.log('状态',status)
			if(status){
				const signAdd = await db.collection('sign').add({
					data:{
						name,
						password
					}
				});
				console.log('云函数注册---------',signAdd)
				return {
					code: 200,
					status: 'S',
					data: signAdd,
					message: '注册成功'
				}
			}else{
				return {
					code: 201,
					status: 'N',
					data: [],
					message: '该账号已注册'
				}
			}
		}catch(e){
			console.log('错误---------',e)
			return {
				code: 20000,
				status: 'N',
				data: [],
				message: '注册失败'
			}
		}
	}else if( type === 'login') {//登录
		const { name, password, openid } = event;
		try{
			const getAdd = await db.collection('sign').where({
				data:{
					name
				}
			}).get();
			const status = getAdd.data.some(item=> item.data.name == name && item.data.password == password);
			if(status){
				const updateAdd = await db.collection('sign').where({
				data:{
					name
				}
			}).update({
					openid,
					data:{
						isLogin: true
					}
				})
				const resumeList = await db.collection('projectList').where({
					openid
				}).get()
				console.log('简历数据有没有',resumeList)
				if(resumeList.data.length> 0){
					return {
						code: 200,
						status: 'S',
						resume: true,
						data: updateAdd,
						message: '登录成功'
					}
				}else{
					return {
						code: 200,
						status: 'S',
						resume: false,
						data: updateAdd,
						message: '登录成功'
					}
				}
				
			}else{
				return {
					code: 201,
					status: 'N',
					data: [],
					message: '密码或账号错误'
				}
			}
		}catch(e){
			return {
				code: 20000,
				status: 'N',
				data: [],
				message: '登录失败'
			}
		}
	}
	 if( type === 'personal'){
		const res = await db.collection('user').get();
		return res;
	}
	 if( type === 'skill'){
		 const ret = await db.collection('skill').get();
		return ret;
	 }
	 if( type === 'project'){
		 const ret1 = await db.collection('project').get();
		 return ret1;
	 }

	
};

